<template>
	<view>
		<form action="">
			<input type="text" placeholder="请输入姓名" v-model="name">
			<input type="password" placeholder="密码" v-model="password">
			<button @click="login">登录</button>
		</form>
	</view>
</template>

<script>
	import request from '@/api/index.js'
	export default {
		data() {
			return {
				name:'蔡徐坤',
				password:123
			}
		},
		methods: {
			login(){
				uni.request({
				    url: 'http://127.0.0.1:8081/login/admin/doLogin',
				    method: 'POST',
				    data: {
				        name:this.name,
				        password: this.password
				    },
				    header: {
				        'Content-Type': 'application/json' // 根据后端接口要求设置请求头
				    },
				    success: function (res) {
				        console.log('请求成功:', res.cookies);
						const cookieArray = res.cookies;
						// 先获取这个数组的全部字符
						const cookieString = cookieArray[0];
						// 通过正则获取satoken
						const match = cookieString.match(/satoken=([^;]+)/);
						if (match) {
						    const satoken= match[1];
							uni.setStorageSync('satoken',satoken);
							uni.navigateTo({
								url:"/pages/index/index"
							})
						}
				    },
				    fail: function (err) {
				        console.error('请求失败:', err);
				    }
				});
			}
		}
	}
</script>

<style>

</style>
